package Server;

import Email.ManageCaptcha;
import MyInterfaces.MassageType;
import Data.PrivateInformationData;
import Data.event;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.math.BigInteger;
import java.net.Socket;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.*;
import java.util.Date;
import java.util.Objects;

public class ChangeUserInformation {
    public ChangeUserInformation(Socket socket) {
        this.socket = socket;
    }

    static String url = "jdbc:mysql://127.0.0.1:3306/nox?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8";
    static String user = "root";
    static String password = "Mi20030610.";
    private Socket socket;
    String sql = "update userinformation set ";
    //userPwd =?,avatar=?,userNickName=?,sex=?,birth=?,saying=? where id=?;";
    public void changeInformation(event massage) throws SQLException, IOException, NoSuchAlgorithmException {
        PrivateInformationData privateInformationData=(PrivateInformationData) massage.getT();

        System.out.println("头像头像");
        if(privateInformationData.getAvatar()!=null)
        {
            //如果得到的头像不为空，说明换头像了
            //需要先写入到文件当中
            System.out.println(privateInformationData.getSuffix_name());
            String path="F:\\qqserverfile\\avatar"+"\\"+privateInformationData.getUserid()+"."+privateInformationData.getSuffix_name();
        //    String sqlpath=path.replace("\\","\\\\");
            File file=new File(path);
            sql=sql+"avatar='"+path.replace("\\","\\\\")+"',";
            FileOutputStream fileOutputStream=new FileOutputStream(file);
            fileOutputStream.write(privateInformationData.getAvatar());
            fileOutputStream.close();
        }

        System.out.println("密码");
        if(!privateInformationData.getPassword().equals(""))
        {
            //如果密码不为空，需要看是否验证码有误
            String yzm= ManageCaptcha.GetcaptchaByEmail(privateInformationData.getEmailNumber(),new Date()).getCaptcha();
            if(yzm.equals(privateInformationData.getCaptcha()))     sql=sql+"userPwd="+getMD5(privateInformationData.getPassword())+",";
            else
            {
                event returnmassage=new event();
                returnmassage.setType(MassageType.Return_Change_Information_Fail);
                ObjectOutputStream writer=new ObjectOutputStream(socket.getOutputStream());
                writer.writeObject(returnmassage);
                writer.flush();

                return;
            }
        }

        System.out.println("昵称");
        if(privateInformationData.getUserNickName().equals(""))
        {
            sql=sql+"userNickName='"+privateInformationData.getUserNickName()+"',";
        }

        System.out.println("性别");
        if(privateInformationData.getSex().equals(""))
        {
            sql=sql+"sex='"+privateInformationData.getSex()+"',";
        }

        System.out.println("生日");
        if(privateInformationData.getBirth()!=null)
        {
            sql=sql+"birth='"+privateInformationData.getBirth()+"',";
        }

        System.out.println("个性签名");
        if(!privateInformationData.getSaying().equals(""))
        {
            sql=sql+"saying='"+privateInformationData.getSaying()+"',";
        }

        System.out.println("修改");
        System.out.println(sql.charAt(sql.length()-1));
        if(sql.charAt(sql.length()-1)==',')
        {
            sql=sql.substring(0,sql.length()-1);
        }

        sql=sql+"where id='"+privateInformationData.getUserid()+"';";
        System.out.println(sql);
        Connection Conn = DriverManager.getConnection(url, user, password);
        PreparedStatement operation = null;
        operation=Conn.prepareStatement(sql);
        int k=operation.executeUpdate();
        if(k>0)
        {
            //说明成功啦
            event returnmassage=new event();
            returnmassage.setType(MassageType.Return_Change_Information_Success);
            returnmassage.setT(privateInformationData);
            ObjectOutputStream writer=new ObjectOutputStream(socket.getOutputStream());
            writer.writeObject(returnmassage);
            writer.flush();
        }
        else
        {
            event returnmassage=new event();
            returnmassage.setType(MassageType.Return_Change_Information_Success);
            returnmassage.setT(privateInformationData);
            ObjectOutputStream writer=new ObjectOutputStream(socket.getOutputStream());
            writer.writeObject(returnmassage);
            writer.flush();
        }
//        System.out.println(privateInformationData.getSuffix_name()+"  "+privateInformationData.getUserid());
//        //先判断头像是否修改
//
//        String path="";
////        if(privateInformationData.getSuffix_name().equals(""))
////        {
////            //如果没有后缀名，说明没有修改图片，就不管
////        }
////        else
////        {
////            //如果修改了
////            //先删掉同名文件
////            File target=new File("E:\\JDK\\QQServer\\nox\\src\\FIle"+"\\"+privateInformationData.getUserid());
////            //如果存在就删除掉
////            if(target.exists())
////            {
////                target.delete();
////            }
////            //再次进去读写
////            path="E:\\JDK\\QQServer\\nox\\src\\FIle"+"\\"+privateInformationData.getUserid()+"."+privateInformationData.getSuffix_name();
////            System.out.println(path);
////            File file=new File(path);
////            FileOutputStream fileOutputStream=new FileOutputStream(file);
////            fileOutputStream.write(privateInformationData.getAvatar());
////            fileOutputStream.close();
////            System.out.println("头像保存成功");
////        }
//        if(!Objects.equals(privateInformationData.getSuffix_name(), ""))
//        {
//            path="E:\\JDK\\QQServer\\nox\\src\\FIle"+"\\"+privateInformationData.getSuffix_name();
//            System.out.println(path);
//            File file=new File(path);
//            FileOutputStream fileOutputStream=new FileOutputStream(file);
//            fileOutputStream.write(privateInformationData.getAvatar());
//            fileOutputStream.flush();
//            fileOutputStream.close();
//            System.out.println("头像保存成功");
//        }
//        Connection Conn = DriverManager.getConnection(url, user, password);
//        PreparedStatement operation = null;
//        if(privateInformationData.getPassword().equals(""))
//        {
//            //如果不需要修改密码
//            sql="update userinformation set avatar=?,userNickName=?,sex=?,birth=?,saying=? where id=?;";
//            operation=Conn.prepareStatement(sql);
//
//            operation.setString(1,path);
//            operation.setString(2,privateInformationData.getUserNickName());
//            operation.setString(3,privateInformationData.getSex());
//            operation.setString(4,privateInformationData.getBirth().toString());
//            operation.setString(5,privateInformationData.getSaying());
//            operation.setString(6,privateInformationData.getUserid());
//        }
//        else
//        {
//            //如果需要修改密码，需要判断验证码是否正确
//            String yzm= ManageCaptcha.GetcaptchaByEmail(privateInformationData.getEmailNumber(),new Date()).getCaptcha();
//            if(!yzm.equals(privateInformationData.getCaptcha()))
//            {
//                event returnmassage=new event();
//                returnmassage.setType(MassageType.Return_Change_Information_Fail);
//                ObjectOutputStream writer=new ObjectOutputStream(socket.getOutputStream());
//                writer.writeObject(massage);
//                writer.flush();
//                return ;
//            }
//
//            operation=Conn.prepareStatement(sql);
//            operation.setString(1,getMD5(privateInformationData.getPassword()));
//
////            path="E:\\JDK\\QQServer\\nox\\src\\FIle"+"\\"+privateInformationData.getUserid();
////            File file=new File(path);
////            if(file.exists())
////            {
////                FileOutputStream fileOutputStream=new FileOutputStream(file);
////                fileOutputStream.write(privateInformationData.getAvatar());
////                fileOutputStream.close();
////            }
////            else path="";
//            operation.setString(2,path);
//            //设置成为路径
//
//            operation.setString(3,privateInformationData.getUserNickName());
//            operation.setString(4,privateInformationData.getSex());
//            operation.setString(5,privateInformationData.getBirth().toString());
//            operation.setString(6,privateInformationData.getSaying());
//            operation.setString(7,privateInformationData.getUserid());
//        }
//        //先查询是否有这个邮箱
//        int k = operation.executeUpdate();
//        if(k>0)
//        {
//            Conn.close();
//            //重置密码成功
//            event returnmassage=new event();
//            returnmassage.setType(MassageType.Return_Change_Information_Success);
//            returnmassage.setT(privateInformationData);
//            System.out.println(privateInformationData.getUserid());
//            ObjectOutputStream writer=new ObjectOutputStream(socket.getOutputStream());
//            writer.writeObject(returnmassage);
//            writer.flush();
//        }
//        else
//        {
//            //重置密码错误
//            event returnmassage=new event();
//            returnmassage.setType(MassageType.Return_Change_Information_Fail);
//            ObjectOutputStream writer=new ObjectOutputStream(socket.getOutputStream());
//            writer.writeObject(returnmassage);
//            writer.flush();
//        }
    }
    public String getMD5(String str) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(str.getBytes());
        return new BigInteger(1, md.digest()).toString(16);
    }
}
